package com.homework.dao;

import com.homework.entity.Cat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("catDao")
public class CatDaoImpl implements CatDao {

    @Autowired
    private JdbcTemplate jtl;

    @Override
    public void insertCat(Cat c) {
        System.out.println("CatDaoImpl.insertCat");
        String sql = "insert into t_cat(cno,cname,cage,species,master) values (seq_test.nextval,?,?,?,?)";
        jtl.update(sql,c.getCname(),c.getCage(),c.getSpecies(),c.getMaster());
    }

    @Override
    public void updateCat(Cat c) {
        System.out.println("CatDaoImpl.updateCat");
        String sql = "update t_cat set cname = ?,cage = ?,species = ?,master = ? where cno = ?";
        jtl.update(sql,c.getCname(),c.getCage(),c.getSpecies(),c.getMaster(),c.getCno());
    }

    @Override
    public void deleteCat(Integer cno) {
        System.out.println("CatDaoImpl.deleteCat");
        String sql = "delete from t_cat where cno = ?";
        jtl.update(sql,cno);
    }

    @Override
    public Cat selectCat(Integer cno) {
        System.out.println("CatDaoImpl.selectCat");
        String sql = "select cno,cname,cage,species,master from t_cat where cno = ?";
        RowMapper<Cat> rowMapper = new BeanPropertyRowMapper<>(Cat.class);
        return jtl.queryForObject(sql, rowMapper, cno);
    }

    @Override
    public List<Cat> selectList() {
        System.out.println("CatDaoImpl.selectList");
        String sql = "select cno,cname,cage,species,master from t_cat order by cno";
        RowMapper<Cat> rowMapper = new BeanPropertyRowMapper<>(Cat.class);
        return jtl.query(sql, rowMapper);
    }
}
